<?php
/**
 * Created by PhpStorm.
 * User: gqc
 * Date: 2017/8/22
 * Time: 11:37
 * 对接智能仓库接口
 */
class Stwmsapi {
    private static $url = 'http://stwmsapi.kokoerp.com/api/?c=sync_main&a=inStock';

    /**
     * @param $datas
     */
    public static function prePush($datas) {
        return json_decode(stwmsapi::http_post(self::$url, $datas), true);
    }

    public static function http_post($url,$data) {
        $handle = curl_init();
        curl_setopt($handle,CURLOPT_URL,$url);
        curl_setopt($handle,CURLOPT_RETURNTRANSFER,TRUE);
        curl_setopt($handle,CURLOPT_USERAGENT,'NEWERP API/1.0');
        curl_setopt($handle,CURLOPT_CONNECTTIMEOUT,60);
        curl_setopt($handle,CURLOPT_TIMEOUT,90);
        curl_setopt($handle,CURLOPT_MAXREDIRS,3);
        curl_setopt($handle,CURLOPT_POST,TRUE);
        curl_setopt($handle,CURLOPT_POSTFIELDS,http_build_query($data));
        curl_setopt($handle,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_0);
        curl_setopt($handle,CURLOPT_HEADER,FALSE);    //不返回头信息
//        curl_setopt($handle,CURLOPT_HTTPHEADER,array('sign_md5:'.md5($sign)));

        $response = curl_exec($handle);

        if(curl_errno($handle)){
            $re          = new \stdClass();
            $re->code    = 100;
            $re->message = '连接主机'.$url.'时发生错误: '.curl_error($handle)."<br/>";
            curl_close($handle);
            return json_encode($re);
        }

        curl_close($handle);

        return $response;
    }
}